package g6.UddiRegistration;

import java.rmi.RemoteException;

import org.apache.juddi.v3.client.ClassUtil;
import org.apache.juddi.v3.client.config.UDDIClientContainer;
import org.apache.juddi.v3.client.transport.Transport;
import org.apache.juddi.v3_service.JUDDIApiPortType;
import org.uddi.api_v3.AuthToken;
import org.uddi.api_v3.BusinessInfo;
import org.uddi.api_v3.BusinessList;
import org.uddi.api_v3.GetAuthToken;
import org.uddi.v3_service.DispositionReportFaultMessage;
import org.uddi.v3_service.UDDIInquiryPortType;
import org.uddi.v3_service.UDDIPublicationPortType;
import org.uddi.v3_service.UDDISecurityPortType;

public class JuddiManager {
	//This variable is used to login in the JUDDI server
	private UDDISecurityPortType security = null;
	//This variable is used to access all the API methods
	private JUDDIApiPortType juddiApi = null;
    //This variable is used to publish (register) a service to the JUDDI server
    private UDDIPublicationPortType publish = null;
    private UDDIInquiryPortType inquiry = null; 
    
    private JuddiPublisher publisher;
    private JuddiSearcher searcher;
    
    //This variable is used to cache user authToken, which is needed for later access to the IUDDI server.
    private static AuthToken myAuthToken = null;
	
    private static String username = "g6";//"root";//
    private static String password =   "e14n6rui9nmie71cso8t9icdk2";//"root";//
    
    public void initialize(){
    	try {
    		String clazz = UDDIClientContainer.getUDDIClerkManager(null).getClientConfig().getUDDINode("default").getProxyTransport();
    		Class<?> transportClass = ClassUtil.forName(clazz, Transport.class);
    		if (transportClass!=null) {
               Transport transport = TransportFactory.getTransport();
               security = transport.getUDDISecurityService();
               juddiApi = transport.getJUDDIApiService();
               publish = transport.getUDDIPublishService();
               inquiry = transport.getUDDIInquiryService();     		
            }
    	} catch (Exception e) {
    		e.printStackTrace();
	    }   
    }
    
    public void authenticateToServer() throws DispositionReportFaultMessage, RemoteException {
    	GetAuthToken getAuthTokenMyPub = new GetAuthToken();
        getAuthTokenMyPub.setUserID(username);
        getAuthTokenMyPub.setCred(password);
        
        // Making API call that retrieves the authentication token for the user
        // You can use the group name and password assigned to you to access the JUDDIv3 server for this course.
        myAuthToken = security.getAuthToken(getAuthTokenMyPub);       

        System.out.println (" User AUTHTOKEN = " + myAuthToken.getAuthInfo());
        
        publisher = new JuddiPublisher(publish, myAuthToken);
        searcher = new JuddiSearcher(inquiry);  
	}
        
    public void publishWebService(String key, String serviceName, String url, String description, String uri, String modelName, String modelDetails) throws DispositionReportFaultMessage, RemoteException{
    	publisher.createBusinessService(searcher.getBusinessEntity(key), serviceName, url, description, uri, modelName, modelDetails);    	
    }
    
    public String createBussinessEntity(String businessName, String description) throws DispositionReportFaultMessage, RemoteException{
    	return publisher.createBusinessEntity(businessName, description);
    }
    
    public void deleteBusinessEntities(String businessName) throws DispositionReportFaultMessage, RemoteException{
    	BusinessList businessList = searcher.findBusiness(businessName);            
        publisher.deleteBusinessEntity(businessList);
    }

	public void printBusinessEntities(String businessName) throws DispositionReportFaultMessage, RemoteException {
		BusinessList businessList = searcher.findBusiness(businessName);            
		if (businessList.getBusinessInfos() != null){
			if (businessList.getBusinessInfos().getBusinessInfo().size() > 0){    		
	    		for (BusinessInfo bi : businessList.getBusinessInfos().getBusinessInfo()){
	            	System.out.println("Key " + bi.getBusinessKey() + " Name " + bi.getName() + " Description " + bi.getDescription());
	            }    		
			} else {
				System.out.println("No services founded");
			}
		}
	}

	public UDDIInquiryPortType getInquiryPortType() {
		// TODO Auto-generated method stub
		return this.inquiry;
	}
}
